// pages/store/store.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeProduct: [],
    userinfo:{},
    currIntegral: {},
    remainPoints: 0,
    pointsDue:0,
    member: '',
    pointsTotal: 0,
    storeType: [],
    userInfo: {
      prizeCount: 1, //如果获取用户信息失败，使用户不能抽奖
      points: 30
    },
    customerHomeInfoResponse: {},
    storeDetail:{},
  },

  onReady: function () {
    const that = this;
    if (wx.getStorageSync('logincode') == 501) {
      that.onLoadDataA(that);
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this
  },

  onShow() {
    const that = this;
    that.setData({ logincode: wx.getStorageSync('logincode') })
    // login.code 为501授权成功 502 未授权 不用调用用户信息接口
    console.log(wx.getStorageSync('logincode'));
    if (wx.getStorageSync('logincode') == 501) {
      //  获取用户信息
      that.api.interface.getCustomerInfo().then(res => {
        wx.setStorageSync('userinfo', res.data.data)
        wx.setStorageSync('customerId', res.data.data.customerId)
        that.setData({
          userinfo: (res.data.data || {}).hasOwnProperty('nickName') ? res.data.data : { nickName: '' }
        })
      })
    }
    that.onLoadData(that);
  },

  onShareAppMessage() {

  },

  onLoadData(that) {
    this.api.interface.integralMallContent().then(result => {
      if (result.data.data.remainPoint) {
        that.setData({
          pointsTotal: result.data.data.remainPoint,
        });
      }
      if (result.data.data.ztrees) {
        that.setData({
          storeType: result.data.data.ztrees,
        });
      }
      if (result.data.data.goodsList) {
        for (let item of result.data.data.goodsList) {
          if (item.discount) {
            item.discountShift = item.discount * 10
            if (!this.data.isVipDay && item.discount <= 0.8) {
              this.setData({ isVipDay: true })
            }
          }
        }
        var goodsList = result.data.data.goodsList;

        that.setData({
          member: result.data.data.memberGrade,
          storeProduct: goodsList,
        });

      }
    })
  },

  onLoadDataA(that) {
    this.api.interface.integralDetails().then(result => {
      if (result.data.data.remainPoint) {
        that.setData({
          remainPoints: result.data.data.remainPoint,
        });
      }
      if (result.data.data.pointsDue) {
        that.setData({
          pointsDue: result.data.data.pointsDue,
        });
      }
      if (result.data.data.currIntegral) {
        that.setData({
          currIntegral: result.data.data.currIntegral,
        });
      }
      if (result.data.data.nextIntegral) {
        that.setData({
          nextIntegral: result.data.data.nextIntegral,
        });
      }
      if (result.data.data.integralDetailList) {
        that.setData({
          pointList: result.data.data.integralDetailList,
        });
      }
    })
  },
  cli_exchange(event){
    return false;
    let that = this;
    var id = event.currentTarget.dataset.id;
    that.api.interface.integralMallGetDetail({
      id:id,
    }).then(result=>{
      console.log(result.data.data,111111111111111111)
      if (result.data.data) {
        that.setData({
          id:id,
          storeDetail: result.data.data,
        });


        if (that.data.storeDetail.convertedNumber >= that.data.storeDetail.canExchangedNumber) {
          that.data.alert.show({
            content: that.data.storeDetail.errorMsg,
          });
          return false;
        }
        if ((that.data.storeDetail.discountPoint ? that.data.storeDetail.discountPoint : that.data.storeDetail.point) > that.data.storeDetail.usableTotal) {
          that.data.alert.show({
            content: '活力值不足，兑换失败',
            closeButtonText: '了解更多活力值获取途径',
            onClosed: function () {
              that.data.popup.show({});
            }
          });
          return false;
        }
        if (1 === that.data.storeDetail.errorCode) {
          that.data.alert.show({
            content: that.data.storeDetail.errorMsg,
          });
          return false;
        }

        if (that.data.storeDetail.premiumNormalType == 8 || that.data.storeDetail.premiumNormalType == 11) {
          // 好友试用福利 新客首单福利
          that.onConfirm(that);
        } else {

          switch (that.data.storeDetail.specNo) {
            case '0':
              wx.redirectTo({
                url: '../addresslist/addresslist?type=1&store_id='+that.data.id,
              });
              //that.onChooseAddress(that);
              break;
            case '1':
            case '2':
              var address = {};
              address.userName = '';
              address.telNumber = '';
              address.provinceName = '';
              address.cityName = '';
              address.countyName = '';
              address.detailInfo = '';
              that.setData({
                address: address,
              });
              that.onConfirm(that);
              break;
          }

        }
      }
    });
  },
  openPoints(events) {
    var userinfo = wx.getStorageSync('userinfo');
    if(!userinfo){
      wx.navigateTo({ url:'../authorization/authorization?act='+5});
      return false;
    }
    wx.navigateTo({
      url: '../points/points',
    });
  },

  openRecord(events) {
    var userinfo = wx.getStorageSync('userinfo');
    if(!userinfo){
      wx.navigateTo({ url:'../authorization/authorization?act='+5});
      return false;
    }
    wx.navigateTo({
      url: '../storerecord/storerecord',
    });
  },
  onStoreDetailTap(event) {
    let that = this;
    var userinfo = wx.getStorageSync('userinfo');
    if(!userinfo){
      wx.navigateTo({ url:'../authorization/authorization?act='+5});
      return false;
    }
    for (let i in that.data.storeProduct) {
      console.log(that.data.storeProduct[i].id,'id - -- -- -- - -- >')
      if (event.currentTarget.dataset.id == that.data.storeProduct[i].id) {
        // app.tapActionLog({
        //     page: '福利中心单个商品点击',
        //     type: '点击',
        //     detail: that.data.storeProduct[i].name
        // });
        break;
      }
    }
    console.log()
    wx.navigateTo({
      url: '../storeDetail/storeDetail?id=' + event.currentTarget.dataset.id,
    });
  },
  openTask(){
    var userinfo = wx.getStorageSync('userinfo');
    if(!userinfo){
      wx.navigateTo({ url:'../authorization/authorization?act='+5});
      return false;
    }
    wx.navigateTo({
      url: '../task/task',
    });
  }
})